package com.example.guli.service.edu.controller.admin;


import com.example.guli.common.base.result.R;
import com.example.guli.service.edu.entity.vo.ChapterVo;
import com.example.guli.service.edu.entity.Chapter;
import com.example.guli.service.edu.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author shunxuan
 * @since 2020-07-16
 */
@CrossOrigin
@Api(description = "04章节管理")
@RestController
@RequestMapping("/admin/edu/chapter")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    @ApiOperation("新增章节")
    @PostMapping("save")
    public R save(
            @ApiParam(value = "章节对象",required = true)
            @RequestBody Chapter chapter
            ){
        boolean result = chapterService.save(chapter);
        if(result){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("报错失败");
        }
    }

    @ApiOperation(value = "更新章节",notes = "根据id更新章节")
    @PutMapping("update")
    public R getById(
            @ApiParam(value = "章节对象",required = true)
            @RequestBody Chapter chapter
    ){
        boolean result = chapterService.updateById(chapter);
        if(result){
            return R.ok().message("修改成功");
        }else {
            return  R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据ID查询章节")
    @GetMapping("get/{id}")
    public R getById(
            @ApiParam(value = "章节id",required = true)
            @PathVariable String id
    ){
        Chapter chapter = chapterService.getById(id);
        if(chapter!=null){
            return R.ok().data("data",chapter);
        }else {
            return  R.error().message("数据不存在");
        }
    }

    @ApiOperation(value = "根据ID删除章节",notes = "根据course_id和chapter_id")
    @DeleteMapping("remove/{id}")
    public R removeById(
            @ApiParam(value = "章节id",required = true)
            @PathVariable String id
    ){
        //TODO 删除视频：VOD
        //在此处调用vod中的删除视频文件的接口

        //删除章节
        boolean result = chapterService.removeChapterById(id);
        if(result){
            return R.ok().message("删除成功");
        }else {
            return  R.error().message("数据不存在");
        }
    }

    @ApiOperation("嵌套章节数据列表")
    @GetMapping("nested-list/{courseId}")
    public R nestedListByCourseId(
            @ApiParam(value = "课程id",required = true)
            @PathVariable String courseId
            ){
        List<ChapterVo> chapterVoList= chapterService.nestedList(courseId);
        return R.ok().data("data",chapterVoList);
    }

}

